package org.yxt.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yxt.springcloud.entities.CommonResult;
import org.yxt.springcloud.handler.MySentinelHandler;

@RestController
@RequestMapping(value = "rate")
public class RateLimitController {

    @GetMapping("byRecourse")
    @SentinelResource(value = "byRecourse", blockHandler = "defaultBlockHandler")
    public CommonResult ecourse() {
        return new CommonResult(1, "success");
    }

    private CommonResult defaultBlockHandler(BlockException e) {
        return new CommonResult(0, "blockHandler --> ", e);
    }

    /**
     * 自定义限流处理配置
     *
     * @return
     */
    @GetMapping(value = "customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = MySentinelHandler.class,
            blockHandler = "blockHandler2Qps"
    )
    public CommonResult customerBlockHandler() {
        return new CommonResult(1, "正常访问");
    }
}
